package com.duoec.magichour.controller.web.front;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.web.BaseWebController;
import com.duoec.magichour.dto.UserInfo;
import com.duoec.magichour.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by ycoe on 17/7/5.
 */
@Controller
@RequestMapping
public class SiteWebController extends BaseWebController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/login", "/login-pwd"}, method = RequestMethod.GET)
    public ModelAndView login() {
        UserInfo userInfo = getUserInfo();
        if (userInfo != null) {
            //转向
            return redirect("/article/release");
        }
        return view("/front/site/login-pwd.ftl");
    }

    @RequestMapping(value = "/login-code", method = RequestMethod.GET)
    public ModelAndView loginWithPwd() {
        UserInfo userInfo = getUserInfo();
        if (userInfo != null) {
            //转向
            return redirect("/article/release");
        }
        return view("/front/site/login-code.ftl");
    }

    @Access
    @RequestMapping(value = "/logout.html", method = RequestMethod.GET)
    public ModelAndView logout() {
        UserInfo user = getUserInfo();
        userService.logout(user);
        return redirect("/login-pwd.html");
    }
}
